﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.UC
{
    public partial class SanPhamTrangChuCtrl : System.Web.UI.UserControl
    {
        public List<SAN_PHAM> ltSanPham;
        public List<LOAI_SAN_PHAM> ltLoaiSP;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ltSanPham = new List<SAN_PHAM>();
                ltLoaiSP = LoaiSanPhamModels.LayLoaiSanPhamTrangChu();
                foreach (var item in ltLoaiSP)
                {
                    List<SAN_PHAM> ltSanPham2 = SanPhamModels.LaySanPhamTheoLoai(item.ID, new List<SAN_PHAM>()).Take(12).ToList();
                    foreach (SAN_PHAM items in ltSanPham2)
                    {
                        ltSanPham.Add(items);

                    }
                }
                content.InnerHtml = GenerateSPTieuBieu(ltSanPham, ltLoaiSP);
            }
            
        }

        private string GenerateSPTieuBieu(List<SAN_PHAM> ltSanPham, List<LOAI_SAN_PHAM> ltLoaiSP)
        {
            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < ltLoaiSP.Count; i++)
            {
                if(ltLoaiSP[i].SAN_PHAM.Count()>0)
                {
                    strBuilder.Append("<div class='row clearfix f-space30'></div>");
                    strBuilder.Append("<div class='container'>");
                    strBuilder.Append("<div class='row'>");
                    strBuilder.Append("<div class='col-lg-12 col-md-12 col-sm-12 col-xs-12 main-column box-block'>");
                    strBuilder.Append("<div class='box-heading'><span>" + ltLoaiSP[i].TenLoai + "</span></div>");
                    strBuilder.Append("<div class='box-content'>");
                    strBuilder.Append("<div class='box-products slide' id='productc" + i + "'>");
                    strBuilder.Append("<div class='carousel-controls'>");
                    strBuilder.Append("<a class='carousel-control left' data-slide='prev' href='#productc" + i + "'><i class='fa fa-angle-left fa-fw'></i></a>");
                    strBuilder.Append("<a class='carousel-control right' data-slide='next' href='#productc" + i + "'><i class='fa fa-angle-right fa-fw'></i></a>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("<div class='carousel-inner'>");
                    int count = 0;
                    for (int j = 0; j < ltSanPham.Count(); j++)
                    {
                        if (ltSanPham[j].IDLoaiSanPham == ltLoaiSP[i].ID)
                        {
                            if (j == 0)
                            {
                                strBuilder.Append("<div class='item active'>");
                                strBuilder.Append("<div class='row box-product'>");
                            }
                            else if (count == 0)
                            {
                                strBuilder.Append("<div class='item'>");
                                strBuilder.Append("<div class='row box-product'>");
                            }
                            count++;

                            strBuilder.Append("<div class='col-lg-3 col-md-3 col-sm-6 col-xs-12'>");
                            strBuilder.Append("<div class='product-block'><div class='image'>");
                            if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                            {
                                strBuilder.Append("<div class='product-label product-sale'><span>SALE</span></div>");
                            }
                            strBuilder.Append("<a class='img' href='ChiTietSP.aspx?ID=" + ltSanPham[j].ID+ "'>");
                            strBuilder.Append("<img alt='product info' src='images/products/" + ltSanPham[j].HINH_SAN_PHAM.First().Hinh + "' title='product title'/></a></div>");
                            strBuilder.Append("<div class='product-meta'>");
                            strBuilder.Append("<div class='name'><a href='Products.aspx'>" + ltSanPham[j].TenSanPham + "</a></div>");
                            if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                            {
                                strBuilder.Append("<div class='big-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                         "{0:0,0}", (ltSanPham[j].DonGia - ((ltSanPham[j].DonGia * ltSanPham[j].GiamGia) / 100))) + "<span class='sym'>đ</span></span></div>");
                            }
                            else
                            {
                                strBuilder.Append("<div class='big-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                         "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span></div>");
                            }
                            
                            strBuilder.Append("<div class='big-btns'>");
                            strBuilder.Append("<a class='btn btn-default btn-view pull-left' href='ChiTietSP.aspx?ID=" + ltSanPham[j].ID + "'>Xem</a> <button class='btn btn-default btn-addtocart pull-right' onclick='ThemVaoGioHang(" + ltSanPham[j].ID + ")'>Thêm</button>");
                            strBuilder.Append("</div>");
                            if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                            {
                                strBuilder.Append("<div class='small-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                             "{0:0,0}", (ltSanPham[j].DonGia - ((ltSanPham[j].DonGia * ltSanPham[j].GiamGia) / 100))) + "<span class='sym'>đ</span></span> ");
                                strBuilder.Append("<span class='price-old'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span> </div>");
                            }
                            else
                            {
                                strBuilder.Append("<div class='small-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                         "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span></div>");
                            }

                            strBuilder.Append(Utils.GenerateRation(ltSanPham[j].DANH_GIA.ToList()));
                            strBuilder.Append("<div class='small-btns'>");
                            strBuilder.Append("<button class='btn btn-default btn-compare pull-left' data-toggle='tooltip' title='Add to Compare' onclick='Compare(" + ltSanPham[j].ID + ")'> <i class='fa fa-retweet fa-fw'></i> </button>");
                            strBuilder.Append("<button class='btn btn-default btn-wishlist pull-left' data-toggle='tooltip' title='Add to Wishlist' onclick='ThemVaoYeuThich(" + ltSanPham[j].ID + ")'> <i class='fa fa-heart fa-fw'></i> </button>");
                            strBuilder.Append("<button class='btn btn-default btn-addtocart pull-left' data-toggle='tooltip' title='Add to Cart' onclick='ThemVaoGioHang(" + ltSanPham[j].ID + ")'><i class='fa fa-shopping-cart fa-fw'></i></button>");
                            strBuilder.Append("</div>");
                            strBuilder.Append("</div>");
                            strBuilder.Append("<div class='meta-back'></div>");
                            strBuilder.Append("</div>");
                            strBuilder.Append("</div>");
                            if (count == 4)
                            {
                                strBuilder.Append("</div>");
                                strBuilder.Append("</div>");
                                count = 0;
                            }
                        }

                    }
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");
                }
            }
            return strBuilder.ToString();
        }
    }
}